package ldh.serialize.register;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import ldh.serialize.Serialized;
import ldh.serialize.type.*;

public class Register {

	private static Register instance = null;
	
	private ConcurrentMap<RegisterKey, Serialized<?>> serializeMap = new ConcurrentHashMap<RegisterKey, Serialized<?>>();
	
	private Register() {
		put(int.class, new IntegerSerialized());
		put(Integer.class, new IntegerSerialized());
		put(Byte.class, new ByteSerialized());
		put(byte.class, new ByteSerialized());
		put(boolean.class, new BooleanSerialized());
		put(Boolean.class, new BooleanSerialized());
		put(Character.class, new CharSerialized());
		put(char.class, new CharSerialized());
		put(Double.class, new DoubleSerialized());
		put(double.class, new DoubleSerialized());
		put(Float.class, new FloatSerialized());
		put(float.class, new FloatSerialized());
		put(Long.class, new LongSerialized());
		put(long.class, new LongSerialized());
		put(short.class, new ShortSerialized());
		put(Short.class, new ShortSerialized());
		put(Date.class, new DateSerialized());
		put(java.sql.Date.class, new Date2Serialized());
		put(String.class, new StrSerialized());
		put(LocalDateTime.class, new LocalDateTimeSerialized());
		put(LocalDate.class, new LocalDateSerialized());
	}
	
	public static Register getInstance() {
		if (instance == null) {
			synchronized(Register.class) {
				if (instance == null) {
					instance = new Register();
				}
			}
		}
		return instance;
	}
	
	public Serialized<?> getSerialized(Class<?> clazz) {
		RegisterKey rk = new RegisterKey(clazz);
		return serializeMap.get(rk);
	}
	
	public Serialized<?> getSerialized(String name) {
		RegisterKey rk = new RegisterKey(name);
		return serializeMap.get(rk);
	}
	
	public void put(Class<?> clazz, Serialized<?> bs) {
		RegisterKey rk = new RegisterKey(clazz);
		serializeMap.put(rk, bs);
	}
	
	
}
